package com.un.ebs.customer.task;

import com.un.ebs.core.UserContext;
import com.un.ebs.core.util.DateUtils;
import com.un.ebs.core.util.SecurityUtils;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.customer.CustomerConfigureCode;
import com.un.ebs.customer.domain.CustomerAndDefunctExt;
import com.un.ebs.customer.domain.DefunctExt;
import com.un.ebs.customer.service.CustomerService;
import com.un.ebs.customer.service.DefunctService;
import com.un.ebs.sys.service.ConfigureService;
import com.un.ebs.sys.service.UserService;
import com.un.ebs.sys.service.WxService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class CustomerMessageNotifyTask {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private DefunctService defunctService;

    @Resource
    private CustomerService customerService;

    @Resource
    private WxService wxService;

    @Resource
    private UserService userService;

    @Resource
    private ConfigureService configureService;

//    @Scheduled(fixedDelay = 1 * 60000 * 30)
    public void sendAnniversariesMsg() {
        List<DefunctExt> defuncts = defunctService.queryAnniversaries();
        //logger.warn("纪念日提醒：" + defuncts.size());

        for (DefunctExt obj : defuncts) {

            UserContext userContext = userService.markUserContext(obj.getCreateAt(), obj.getTenantId(), null);
            if (userContext == null) {
                userContext = userService.markUserContext(userService.queryByTenantAdminUserId(obj.getTenantId()), obj.getTenantId(), null);
                if (userContext == null) {
                    logger.warn(String.format("撤销过期订单时，没有一个有效的租户【%s】管理员账号可用。", obj.getTenantId()));
                    continue;
                }
            }
            SecurityUtils.setUserContent(userContext);

            String id = configureService.getValueByCode(CustomerConfigureCode.defunct_day_notify_template_id);
            String config = configureService.getValueByCode(CustomerConfigureCode.defunct_day_notify_template_config);
            String url = configureService.getValueByCode(CustomerConfigureCode.defunct_day_notify_template_url);

            if (StringUtils.isNullOrEmpty(id)) {
                return;
            }

            Date day;
            boolean isBirthday=false;
            if (obj.getBirthday()!=null && DateUtils.formatDate(obj.getBirthday(), "MMdd").equals(DateUtils.formatDate(DateUtils.plusDay(2), "MMdd"))) {
                day = obj.getBirthday();
                isBirthday=true;
            } else if (DateUtils.formatDate(obj.getDefunctDate(), "MMdd").equals(DateUtils.formatDate(DateUtils.plusDay(2), "MMdd"))) {
                day = obj.getDefunctDate();
                isBirthday=false;
            } else {
                return;
            }
            for (CustomerAndDefunctExt item : customerService.queryCustAndDefunctByDefunctId(obj.getId())) {
                wxService.sendMessage(
                        obj.getTenantId(),
                        item.getCustUserId(),
                        id,
                        String.format("尊敬的%s先生/女士！\r\n\r\n       %s是%s的阴生/忌日；如您当天不便前来园区，可委托我们在福位前替您送上献花，点上相思灯，以表思念之情，详情可来电咨询。", item.getCustName(), DateUtils.formatDate(day,"yyyy-M-d"),item.getDefunctName()),
                        item.getCustName(),
                        "纪念日温馨提醒",
                        StringUtils.isNullOrEmpty(item.getTombSkuNameAs()) ? "灵山福寿园" : String.format("%s",item.getTombSkuNameAs()),
                        "客服中心咨询电话：80868160");
            }

            if(isBirthday){
                defunctService.updateBirthdayNotify(obj.getId());
            }else{
                defunctService.updateDefunctDateNotify(obj.getId());
            }
        }
    }
}
